package main

import (
	"fmt"
	"strconv"
	"time"

	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
)

func (m *EnvModel) ItemCount() int {
	return len(m.items)
}

func (m *EnvModel) Value(index int) interface{} {
	return m.items[index].value
}

//血瓶下拉框
func NewBloodComBox() *EnvModel {
	items := [...]string{"每秒吃血瓶", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"}
	m := &EnvModel{items: make([]EnvItem, len(items))}
	for i, v := range items {
		if i > 0 {
			m.items[i] = EnvItem{"秒 " + v + " * 4 次", v}
		} else {
			m.items[i] = EnvItem{v, v}
		}
	}
	return m
}

//职业下拉框
func NewPorfComBox() *EnvModel {
	//items := [...]string{"职业", "战士", "法师", "魔剑士", "圣导师"}
	items := [...]string{"连击职业", "战士"}
	m := &EnvModel{items: make([]EnvItem, len(items))}
	for i, v := range items {
		m.items[i] = EnvItem{v, v}
	}
	return m
}

//初始化窗口
func (this *MyMainWindow) init() (err error) {
	this.MainWindow = new(walk.MainWindow)
	this.mw.AssignTo = &this.MainWindow
	this.mw.Layout = VBox{}
	this.mw.Maximize = true
	this.mw.FixedSize = true
	this.initWidget()

	if err = this.mw.Create(); err != nil {
		return err
	}
	if ic, err := walk.NewIconFromFile("icon.ico"); err != nil {
		return err
	} else {
		this.MainWindow.SetIcon(ic)
	}

	this.MainWindow.SetTitle("回血 连击")
	this.MainWindow.SetBounds(walk.Rectangle{400, 200, 300, 100})
	this.MainWindow.SetMinMaxSize(walk.Size{300, 100}, walk.Size{300, 100})
	return nil
}

func (this *MyMainWindow) run() int {
	return this.MainWindow.Run()
}

//初始化控件
func (this *MyMainWindow) initWidget() (err error) {
	this.mw.Children = []Widget{
		HSplitter{
			Children: []Widget{
				ComboBox{
					AssignTo:     &this.ui.bloodComBox,
					Model:        this.bloodModel,
					CurrentIndex: 0,
				},
				ComboBox{
					AssignTo:     &this.ui.porfComBox,
					Model:        this.porfModel,
					CurrentIndex: 0,
				},
			},
		},
		HSplitter{
			Children: []Widget{
				LineEdit{
					AssignTo:  &this.ui.speedLineEdit,
					CueBanner: "攻击速度大于(60)的数字",
					Font: Font{
						PointSize: 10,
						Family:    "微软雅黑",
					},
					MaxLength: 3,
				},
			},
		},
		HSplitter{
			Children: []Widget{
				LineEdit{
					AssignTo:  &this.ui.speakLineEdit,
					CueBanner: "广告文字",
					Font: Font{
						PointSize: 10,
						Family:    "微软雅黑",
					},
				},
			},
		},
		HSplitter{
			Children: []Widget{
				PushButton{
					AssignTo: &this.ui.startBtn,
					Text:     "启动/停止 未启动",
					MinSize:  Size{0, 30},
				},
				PushButton{
					AssignTo: &this.ui.speakBtn,
					Text:     "自动广告 已关闭",
					MinSize:  Size{0, 30},
				},
			},
		},
	}
	return nil
}

//初始化事件
func (this *MyMainWindow) initEvent() {
	if this.data == nil {
		this.data = &ConstData{}
	}
	ui := this.ui
	//启动按钮
	ui.startBtn.Clicked().Attach(func() {
		if !this.data.startToStop {
			this.data.startToStop = true
			ui.startBtn.SetText(fmt.Sprint("启动/停止", " 已启动"))
			this.bloodFunc = this.bloodPoll
			this.porfFunc = this.porfPoll
			go this.bloodFunc()
			go this.porfFunc()
		} else {
			this.data.startToStop = false
			ui.startBtn.SetText(fmt.Sprint("启动/停止", " 未启动"))
			this.bloodFunc = nil
			this.porfFunc = nil
		}
	})
	//启动按钮
	ui.speakBtn.Clicked().Attach(func() {
		if !this.data.speakOffOn {
			this.data.speakOffOn = true
			ui.speakBtn.SetText(fmt.Sprint("自动广告", " 已开启"))
			this.speakFunc = this.speakPoll
			go this.speakFunc()
		} else {
			this.data.speakOffOn = false
			ui.speakBtn.SetText(fmt.Sprint("自动广告", " 已关闭"))
			this.speakFunc = nil
		}
	})
}

//论询技能
func (this *MyMainWindow) porfPoll() {
	for {
		index := this.ui.porfComBox.CurrentIndex()
		value := this.porfModel.Value(index).(string)
		speedTxt := this.ui.speedLineEdit.Text()
		speed, err := strconv.Atoi(speedTxt)
		if index == 0 || err != nil || speed < 60 {
			sleep(time.Second)
			continue
		}
		slp := (time.Second * 60) / time.Duration(speed)
		sleep(slp)
		if this.data.startToStop {
			this.porfVmKey(slp, value)
		} else {
			break
		}
	}
}

//论询加血
func (this *MyMainWindow) bloodPoll() {
	var (
		bloodMode = this.ui.bloodComBox.Model().(*EnvModel)
		index     int
		value     string
	)
	for {
		index = this.ui.bloodComBox.CurrentIndex()
		value = bloodMode.Value(index).(string)
		s, err := strconv.Atoi(value)
		if index == 0 || err != nil {
			sleep(time.Second)
			continue
		}
		sleep(time.Second / time.Duration(s))
		if this.data.startToStop {
			this.bloodVmKey()
		} else {
			break
		}
	}
}

//自动发言
func (this *MyMainWindow) speakPoll() {
	for {
		if this.data.speakOffOn {
			sleep(time.Second * 20)
			if this.ui.speakLineEdit.Text() != "" {
				//广告
				this.speakVmKey()
			}
		} else {
			break
		}
	}
}

func sleep(d time.Duration) {
	time.Sleep(d)
}
